<style>
.pure_table_extra { width: 1100px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
.ui-autocomplete-loading { background: #fff url('../3rd_party/jquery/images/gg_load.gif') no-repeat right center; }
</style>

<?php
$pg = Utils::get_param('pg', 1);
$data = $orm->get('pb_colg_student', 't.id_number', 'DESC', $pg, SIZE, NULL);
?>

<div class="pure-form">
  <input id="keywords" type="text" style="width: 250px;" placeholder="{Keywords}" onkeypress="if(event.which == 13) search(1, 'id_number', 'ASC');">
  <button onclick="search(1, 'id_number', 'ASC');" class="pure-button">Search</button>
</div>
<div>&nbsp;</div>
<button onclick="Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=index_student&pg=' + __data.p);" class="pure-button pure-button-primary">New Student</button>
<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'id_number', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">ID Number</th>
      <th style="width: 80px;" class="ui-widget-header" onclick="return search(1, 'first_name', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">First Name</th>
      <th style="width: 80px;" class="ui-widget-header" onclick="return search(1, 'last_name', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Last Name</th>
      <th style="width: 100px;" class="ui-widget-header" onclick="return search(1, 'preferred_name', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Preffered Name</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'date_of_birth', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">DoB</th>
      <th style="width: 80px;" class="ui-widget-header" onclick="return search(1, 'phone', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Phone</th>
      <th style="width: 80px;" class="ui-widget-header" onclick="return search(1, 'mobile', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Mobile</th>
      <th class="ui-widget-header" onclick="return search(1, 'email', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Email</th>
      <th style="width: 100px;" class="ui-widget-header" onclick="return search(1, 'user_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">User</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'date', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Date</th>
      <th style="width: 40px;" class="ui-widget-header">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  
  $(function() {
    $('#keywords').autocomplete({
      source: __requrl + 'autocomplete',
      minLength: 2,
      select: function(event, ui) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=index_student&id=' + ui.item.id + '&pg=' + 1);
      }
    });
    render();
  });

  function render() {
    $('#pagination').pagination({
      items: __data.t,
      itemsOnPage: __data.s,
      currentPage: __data.p,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        search(pageNumber, __data.o, __data.q);
      }
    });
    render_data();
  };
  
  function render_data() {
    Url.insert_param('pg', __data.p);
    Url.insert_param('o', __data.o);
    Url.insert_param('q', __data.q);
    
    $('#data_body').empty();
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#data_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.id_number + '</td>' +
          '<td title="' + itm.first_name + '">' + Utils.get_short(itm.first_name, 8) + '</td>' +
          '<td title="' + itm.last_name + '">' + Utils.get_short(itm.last_name, 8) + '</td>' +
          '<td title="' + itm.preferred_name + '">' + Utils.get_short(itm.preferred_name, 8) + '</td>' +
          '<td>' + itm.date_of_birth + '</td>' +
          '<td>' + itm.phone + '</td>' +
          '<td>' + itm.mobile + '</td>' +
          '<td>' + itm.email + '</td>' +
          '<td>' + itm.user_title__ + '</td>' +
          '<td>' + itm.date + '</td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="Url.redirect(' + __redmd + ', \'' + __sys + '.php?i=' + __id + '&__file=index_student&id=' + itm.id + '&pg=' + __data.p + '\');"></span>' +
            '<span style="display: none; cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="delete_obj(' + itm.id + ');"></span>' +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.d.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=9>None</td></tr>');
    }
  };
  
  function delete_obj(id) {
    var req = Utils.get_by_id(id, __data.d);
    req.__pg = __data.p;
    
    Ajax.request(
      __requrl + 'delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      { confirm: 1 }
    );
  };

  function search(pg, o, q) {
    var req = {
      term: $('#keywords').val(),
      pg: pg,
      q: q,
      o: o,
    }
    Ajax.request(
      __requrl + 'search',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
    return false;
  };
  /*]]>*/
</script>